package com.robotic.test;

import lombok.Data;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

// For command 0x21050F0A: Battery Information
@Data
public class BatterySensorData {
    private final int voltage;
    private final short current;
    private final int remainingCapacity;
    private final int nominalCapacity;
    private final int cycles;
    private final int productionDate;
    private final int balancedLow;
    private final int balancedHigh;
    private final int protectedState;
    private final short softwareVersion;
    private final short batteryLevel;
    private final short mosState;
    private final short batteryQuantity;
    private final short batteryNtc;
    private final float[] batteryTemperature;

    public BatterySensorData(byte[] data) {
        ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
        this.voltage = Short.toUnsignedInt(buffer.getShort());
        this.current = buffer.getShort();
        this.remainingCapacity = Short.toUnsignedInt(buffer.getShort());
        this.nominalCapacity = Short.toUnsignedInt(buffer.getShort());
        this.cycles = Short.toUnsignedInt(buffer.getShort());
        this.productionDate = Short.toUnsignedInt(buffer.getShort());
        this.balancedLow = Short.toUnsignedInt(buffer.getShort());
        this.balancedHigh = Short.toUnsignedInt(buffer.getShort());
        this.protectedState = Short.toUnsignedInt(buffer.getShort());
        this.softwareVersion = (short) Byte.toUnsignedInt(buffer.get());
        this.batteryLevel = (short) Byte.toUnsignedInt(buffer.get());
        this.mosState = (short) Byte.toUnsignedInt(buffer.get());
        this.batteryQuantity = (short) Byte.toUnsignedInt(buffer.get());
        this.batteryNtc = (short) Byte.toUnsignedInt(buffer.get());
        this.batteryTemperature = new float[4];
        for (int i = 0; i < 4; i++) {
            this.batteryTemperature[i] = buffer.getFloat();
        }
    }

    // Manual getter methods to resolve compilation issues
    public int getVoltage() {
        return voltage;
    }

    public short getCurrent() {
        return current;
    }

    public short getBatteryLevel() {
        return batteryLevel;
    }

    @Override
    public String toString() {
        // Assuming jy_exe version is 1.10.19 or newer
        return "BatterySensorData{" +
                "voltage=" + voltage / 100.0 + "V" + // As per documentation, it's in V, but often scaled
                ", current=" + current * 10 + "mA" +
                ", remainingCapacity=" + remainingCapacity + "mAh" +
                ", nominalCapacity=" + nominalCapacity + "mAh" +
                ", cycles=" + cycles +
                ", batteryLevel=" + batteryLevel + "%" +
                ", batteryTemperature=" + Arrays.toString(batteryTemperature) + "°C" +
                '}';
    }
}
